package com.hxm.appGenealogy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxm.appGenealogy.Function.FamilyFunction;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.config.FamilyMergeConfig;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.Family;
import com.hxm.appGenealogy.entity.FamilyVo;
import com.hxm.appGenealogy.entity.People;
import com.hxm.appGenealogy.entity.ResponDataVo;
import com.hxm.appGenealogy.entryVo.FamilyMerge;
import com.hxm.appGenealogy.entryVo.FamilyMergeVo;
import com.hxm.appGenealogy.entryVo.MoveVo;
import com.hxm.appGenealogy.mapper.FamilyMapper;
import com.hxm.appGenealogy.mapper.FamilyMergeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("app/familymerge")
public class FamilyMergeController {


    @Autowired
    private FamilyMergeMapper familyMergeMapper;

    @Autowired
    private PeopleFunction peopleFunction;

    @Autowired
    private FamilyMergeConfig familyMergeConfig;

    @Autowired
    private FamilyFunction familyFunction;

    /**
     * 添加合并的信息;
     *
     * @param familyMergeVo
     * @return
     */
    @PostMapping("add")
    public ResponDataVo add(@RequestBody FamilyMergeVo familyMergeVo) {
        String parmAnalysis = ParmIntended.parmAnalysis(familyMergeVo, "peopleId", "familyId", "familyMergeId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前操作人的权限;
        People people = peopleFunction.peopleGetByid(familyMergeVo.getPeopleId());
        if (people == null) {
            return new ResponDataVo("请先登录;");
        }
        //判断当前的这个人是否有权限来进行操作;
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限来进行合并");
        }
        Family family = (Family)familyFunction.selectByid(familyMergeVo.getFamilyId()).getData();
        if(family==null){
            return new ResponDataVo("不存在当前的家族的信息;");
        }
        if(family.getId().equals(familyMergeVo.getFamilyMergeId())){
            return new ResponDataVo("自己不可以加入自己");
        }
        //判断我是否已经加入过别的家族了;
        List<FamilyMerge> familyMergeList = familyMergeMapper.selectList(new QueryWrapper<FamilyMerge>().eq("family_merge_id", familyMergeVo.getFamilyMergeId()));
        if(familyMergeList.size()>0){
            return new ResponDataVo("您已经加入到了别的家族;");
        }
        //添加合并的信息;
        FamilyMerge familyMerge = new FamilyMerge();
        familyMerge.setFamilyMergeId(familyMergeVo.getFamilyMergeId());
        familyMerge.setFamilyId(familyMergeVo.getFamilyId());
        familyMerge.setTime(new Date());
        //设置为没有审核完成;
        familyMerge.setIsYes(0);
        //设置审核的状态的信息;
        familyMerge.setCheckState(3);
        familyMergeMapper.insert(familyMerge);
        return new ResponDataVo(familyMerge);
    }


    /**
     * 对合并过来的家族来进行审核;
     * 迁移相册数据;
     * 迁移动态数据;
     */
    @PostMapping("checkstatus")
    public ResponDataVo checkStatus(@RequestBody FamilyMergeVo familyMergeVo) {
        //参数信息的教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(familyMergeVo, "peopleId", "id", "checkState");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前登录的人有没有权限;
        People people = peopleFunction.peopleGetByid(familyMergeVo.getPeopleId());
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限来进行审核;");
        }
        /*familyMergeMapper.selectOne(new QueryWrapper<>().eq(""))*/
        FamilyMerge familyMerge = familyMergeMapper.selectById(familyMergeVo.getId());
        //获取当前的审核的状态;
        //0是审核失败
        if (familyMergeVo.getCheckState() == 0) {
            familyMerge.setCheckState(0);
            familyMergeMapper.updateById(familyMerge);
            return new ResponDataVo(true);
        }
        //审核成功;0
        familyMerge.setCheckState(1);
        familyMergeMapper.updateById(familyMerge);
        //将当前的家族的相册数据进行迁移,迁移到新的家族的下面;
        familyMergeConfig.moveImage(familyMerge);
        //修改家族空间的使用的信息;
        familyMergeConfig.updateSpace(familyMerge);
        //返回当前迁移的加入的人员的信息数据;
        List<People> peopleList = peopleFunction.queryGeListString(JSON.toJSONString(new QueryWrapperVO().eq("family_id", familyMerge.getFamilyMergeId())));
        return new ResponDataVo(peopleList);
    }

    /**
     * 查询迁移家族的人员的信息;
     */
    @PostMapping("peoplemigration")
    public ResponDataVo peopleMigration(@RequestBody Family family) {
        String parmAnalysis = ParmIntended.parmAnalysis(family, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        QueryWrapper<FamilyMerge> familyMergeQueryWrapper = new QueryWrapper<>();
        familyMergeQueryWrapper.eq("family_id", family.getId()).eq("check_state", "1");
        List<FamilyMerge> familyMergeList = familyMergeMapper.selectList(familyMergeQueryWrapper);
        List<People> peopleList = new ArrayList<>();
        familyMergeList.forEach(t -> {
            if (t == null) {
                return;
            }
            Long familyMergeId = t.getFamilyMergeId();
            List<People> peopleListPeople = peopleFunction.queryGeListString(JSON.toJSONString(new QueryWrapperVO().eq("family_merge_id", familyMergeId)));
            peopleListPeople.forEach(p -> {
                if (p != null) {
                    peopleList.add(p);
                }
            });
        });
        List<People> peopleListJoin = peopleFunction.queryGeListString(JSON.toJSONString(new QueryWrapperVO().eq("family_merge_id", family.getId())));
        peopleListJoin.forEach(j -> {
            if (j != null) {
                peopleList.add(j);
            }
        });
        return new ResponDataVo(peopleList);
    }


    /**
     * 人员信息的迁移;
     * @param moveVo
     * @return
     */
    @PostMapping("movePeople")
    public ResponDataVo movePeople(@RequestBody MoveVo moveVo) {
        String parmAnalysis = ParmIntended.parmAnalysis(moveVo, "seatPeopleId", "peopleMoveId", "peopleId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        People peopleLogin = peopleFunction.peopleGetByid(moveVo.getPeopleId());
        if(peopleLogin==null){
            return new ResponDataVo("没有当前人的信息;");
        }
        if (peopleLogin.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限来进行迁移人员的数据;");
        }
        //判断当前的坑位是都已经有真人的信息的数据;
        People people = peopleFunction.peopleGetByid(moveVo.getSeatPeopleId());
        ////0是在树,1是不在树;
        if (people.getIsHavePit() == 0) {
            return new ResponDataVo("当前tree的节点已经存在真实人员的数据了;");
        }
        return familyMergeConfig.MovePeople(moveVo);
    }


    /**
     * 查询出迁移家族中已经迁移完成还有没有开始呢迁移的家族的数据;
     */
    @PostMapping("slectFamilyMerge")
    public ResponDataVo slectFamilyMerge(@RequestBody FamilyMerge familyMerge) {
        String parmAnalysis = ParmIntended.parmAnalysis(familyMerge, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        List<FamilyMerge> familyMergeList = familyMergeMapper.selectList(new QueryWrapper<FamilyMerge>().eq("family_id",familyMerge.getId()));
       /*List<Family> familyList = new ArrayList<>();
        familyMergeList.forEach(t -> {
            Family family = (Family) familyFunction.selectByid(t.getFamilyMergeId()).getData();
            familyList.add(family);
        });*/
        return new ResponDataVo(familyMergeList);
    }

    /**
     * 将迁移家族的数据设置为已经完成的状态,修改迁移家族的数据;
     * @param familyMerge id必须包含迁移家族的id;
     * @return
     */
    public ResponDataVo updateByid(@RequestBody FamilyMerge familyMerge) {
        String parmAnalysis = ParmIntended.parmAnalysis(familyMerge, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        try {
            familyMerge.setCheckState(1);
            familyMergeMapper.updateById(familyMerge);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponDataVo("美女服务器异常哦;");
        }
        return new ResponDataVo(true);
    }


}













